<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The action that provide a solution to paginate the list/stream/content.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  FIXME: Fixes the 'subaction' and 'subactoinargs' location.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_action.php';
require_once INSTALLDIR . '/lib/slh_util.php';

// Definitions.
define('DEFAULT_PER_PAGE',				5);


class PaginationAction extends Action
{
	/**
	 * Page of the profile users list we're on, default = 1.
	 */
	var $page = null;
	
	/**
	 * If has more content in the next page.
	 */
	var $hasnextpage = false;
	
	/**
	 * Items showed in one page.
	 */
	var $itemsperpage;

	/**
	 * Construtors.
	 * 
	 * @param $itemsperpage.
	 */
	function __construct($itemsperpage = DEFAULT_PER_PAGE)
	{
		parent::__construct();
		
		$this->itemsperpage = $itemsperpage;
	}
	
	function prepare($args)
	{
		parent::prepare($args);
		
		$this->page = ($this->getTrimmedArg('page')) ? intval($this->getTrimmedArg('page')) : 1;
		if (0 == $this->page) {
			 $this->clientError('没有找到这一页哦,亲.', 404);
		}
		
		return true;
	}
	
// Overrided ends //////////////////////////////////////////////////////////////
	/**
	 * Show the pagination labels.
	 */
	function showPagination()
	{
		// Pagination.
		$this->pagination($this->page > 1, $this->hasnextpage, 
						  $this->page, $this->subaction);
	}
	
	/**
	 * Check the given ids if meeting the pagination requirements,
	 * and then decide whether paginate or not, if so, remove the 
	 * last item for it's just a switch factor.
	 */
	function doPagination(&$ids)
	{
		if (count($ids) > $this->itemsperpage) {
        	$this->hasnextpage = true;
        	array_pop($ids);
        } else {
        	$this->hasnextpage = false;
        }
	}
	
	/**
	 * Check the validation of the pagination.
	 * TODO: the error presentation.
	 */
	function checkPagination($itemcnt)
	{
		if($this->page > 1 && $itemcnt == 0) {
            $this->serverError(_('没有内容了呃,亲!'), 404);
        }
	}
}

?>